package sedh.project
import grails.plugins.springsecurity.Secured;


class MunicipioController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }
	
	
	def searchMunicipio = {
		def list = Municipio.findAllByNomeIlike(wrapSearchParm(params.searchvalue), [sort:'nome',order:'asc'])
		render(template:'searchResult', model:[searchresults : list])
	}
	
	def String wrapSearchParm(value) {
		'%'+value+'%'
	   }
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def list = {
		
		def municipioInstance = Municipio.get(params.id)
		params.sort = "nome"
		params.order = "asc"
		
		if (municipioInstance) {
			def servicosList = AlocadorMunSer.findAllByMunicipio(municipioInstance)
			def programasList = AlocadorMunProg.findAllByMunicipio(municipioInstance)
			[servicosList: servicosList, programasList: programasList, municipioInstance: municipioInstance, municipioInstanceList: Municipio.list(params), municipioInstanceTotal : Municipio.count()]
			
		} else {
			[municipioInstanceList: Municipio.list(params), municipioInstanceTotal : Municipio.count()]
		}
    }
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def create = {
        def municipioInstance = new Municipio()
        municipioInstance.properties = params
        return [municipioInstance: municipioInstance]
    }

    def save = {
		
		params.nome = params.nome.toLowerCase().split(' ').collect{ it.capitalize() }.join(' ')
        def municipioInstance = new Municipio(params)
        if (municipioInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'municipio.label', default: 'Municipio'), municipioInstance.id])}"
            redirect(action: "show", id: municipioInstance.id)
        }
        else {
            render(view: "create", model: [municipioInstance: municipioInstance])
        }
    }
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def show = {
		redirect(action: "list", id: params.id)
    }
	
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def edit = {
        def municipioInstance = Municipio.get(params.id)
        if (!municipioInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [municipioInstance: municipioInstance]
        }
    }
	
    def update = {
        def municipioInstance = Municipio.get(params.id)
        if (municipioInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (municipioInstance.version > version) {
                    
                    municipioInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'municipio.label', default: 'Municipio')] as Object[], "Another user has updated this Municipio while you were editing")
                    render(view: "list", model: [municipioInstance: municipioInstance])
                    return
                }
            }
            municipioInstance.properties = params
            if (!municipioInstance.hasErrors() && municipioInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'municipio.label', default: 'Municipio'), municipioInstance.id])}"
                redirect(action: "show", id: municipioInstance.id)
            }
            else {
                render(view: "list", model: [municipioInstance: municipioInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
            redirect(action: "list")
        }
    }
	
	@Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def delete = {
        def municipioInstance = Municipio.get(params.id)
		
		def servicosParaMun = AlocadorMunSer.findAllByMunicipio(municipioInstance)
		def programasParaMun = AlocadorMunProg.findAllByMunicipio(municipioInstance)
		
		for (AlocadorMunSer aloc : servicosParaMun) {
			if (aloc) {
				try {
					aloc.delete(flush: true)
					flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
					
				}
				catch (org.springframework.dao.DataIntegrityViolationException e) {
					flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
					
				}
			}
		}
		for(AlocadorMunProg aloc : programasParaMun) {
			if (aloc) {
				try {
					aloc.delete(flush: true)
					flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
					
				}
				catch (org.springframework.dao.DataIntegrityViolationException e) {
					flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
					
				}
			}
		}

        if (municipioInstance) {
            try {
                municipioInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'municipio.label', default: 'Municipio'), params.id])}"
            redirect(action: "list")
        }
    }
}
